package com.hoshiicloud.user.controller.company;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.validate.ValidGroupAdd;
import com.hoshiicloud.common.validate.ValidGroupUpdate;
import com.hoshiicloud.user.controller.BaseController;
import com.hoshiicloud.user.entity.company.AttachmentType;
import com.hoshiicloud.user.param.company.AttachmentTypePageParam;
import com.hoshiicloud.user.service.company.IAttachmentTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 附件类型 前端控制器
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Slf4j
@RestController
@RequestMapping("/company/attachtype")
@Api(value = "附件类型管理", tags = {"附件类型操作接口"})
public class AttachmentTypeController extends BaseController {

    @Autowired
    private IAttachmentTypeService attachTypeService;

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询附件类型信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询",
            notes = "附件类型信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response findAttachById(@PathVariable("id") Long id) {
        return Response.data(attachTypeService.getById(id));
    }

    /**
     * 根据ID组查询附件类型信息
     *
     * @param ids ID组
     * @return Response<List < AttachmentType>>
     */
    @GetMapping("/getByIds")
    @ApiOperation(value = "根据ID组查询", notes = "根据ID组查询附件类型信息", consumes = MediaType.ALL_VALUE)
    public Response<List<AttachmentType>> getByIds(@RequestParam("ids") List<Long> ids) {
        return Response.data(attachTypeService.getByIds(ids));
    }

    /**
     * @param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据名称查询
     */
    @GetMapping("/find/{name}")
    @ApiOperation(value = "查询",
            notes = "根据名称查询附件类型信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response findAttachByName(@PathVariable("name") String name) {
        Wrapper<AttachmentType> wrapper = Wrappers.<AttachmentType>query().lambda().like(AttachmentType::getName, name);
        return Response.data(attachTypeService.list(wrapper));

    }


    /**
     * 分页查询附件类型信息（post请求，以对象接收参数）
     *
     * @param param 查询参数实体
     * @return Response<IPage < AttachmentType>>
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询附件类型信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<IPage<AttachmentType>> list(@RequestBody AttachmentTypePageParam param) {
        try {
            QueryWrapper<AttachmentType> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getName() != null), AttachmentType::getName, param.getName());
                    // .eq((param.getCode() != null), AttachmentType::getCode, param.getCode());
            IPage<AttachmentType> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(attachTypeService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询附件类型信息异常", e);
            return Response.failed("分页查询附件类型信息异常");
        }
    }


    /**
     * @param attachmentType
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增",
            notes = "新增附件类型信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response saveAttach(@RequestBody @Validated(value = ValidGroupAdd.class) AttachmentType attachmentType) {
        return Response.data(attachTypeService.save(attachmentType));
    }

    /**
     * @param attachmentType
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改",
            notes = "修改附件类型信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response updateAttachById(@RequestBody @Validated(value = ValidGroupUpdate.class) AttachmentType attachmentType) {
        return Response.data(attachTypeService.updateById(attachmentType));
    }


    /**
     * 根据ID删除附件类型信息
     * @param id 附件类型ID
     * @return Response<Boolean>
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除附件类型信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "附件类型ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        return Response.data(attachTypeService.delete(id));
    }


    @PostMapping("/query/id")
    @ApiOperation(value = "根据类目Id与供应商类型Id查询附件信息", notes = "根据类目Id与供应商类型Id查询附件信息", consumes = MediaType.ALL_VALUE)
    // @ApiImplicitParams({@ApiImplicitParam(name = "categoryIds", value = "类目ID集合", dataType = "List", required = true, example = "", paramType = "query"),
    //         @ApiImplicitParam(name = "organizationTypeId", value = "供应商类型ID", dataType = "Long", required = true, example = "", paramType = "query")})
    // public Response selectAttachmentTypeByCategoryIdOrOrganizationId(@RequestBody AttachmentTypeRequestParam attachmentTypeRequestParam) {
    public Response selectAttachmentTypeByCategoryIdOrOrganizationId(@RequestParam("categoryIds") List<Long> categoryIds, @RequestParam("organizationTypeId") Long organizationTypeId) {
        // return Response.data(attachTypeService.selectAttachmentTypeByCategoryIdOrOrganizationTypeId(attachmentTypeRequestParam.getCategoryIds(), attachmentTypeRequestParam.getOrganizationTypeId()));
        return Response.data(attachTypeService.selectAttachmentTypeByCategoryIdOrOrganizationTypeId(categoryIds, organizationTypeId));
    }


}
